﻿Partial Public Class MouseClicks

    Private Sub btnLoneRanger_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLoneRanger.Click
        'wired up with Handles
        Debug.WriteLine("Lone Ranger Clicked: " & e.Source.ToString)
        e.Handled = False
    End Sub

    Private Sub Bottom_StackPanel_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'wired up in Window Loaded
        Debug.WriteLine("Bottom StackPanel PreviewMouseDown: " & e.Source.ToString)
        e.Handled = False
    End Sub

    Private Sub Bottom_StackPanel_PreviewMouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'wired up in Window Loaded
        Debug.WriteLine("Bottom StackPanel PreviewMouseUp: " & e.Source.ToString)
        'NEVER handle this event, because the button will be in an unstable state
        e.Handled = False
    End Sub

    Private Sub Bottom_StackPanel_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        'wired up in Window Loaded

        'why doesn't this event fire?  Change code in Loaded, Button.MouseDownEvent and set HandledEventsToo = True.  OK, why does this fire now?
        Debug.WriteLine("Bottom StackPanel MouseDown: " & e.Source.ToString)
        e.Handled = False
    End Sub

    Private Sub MouseClicks_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

        Me.Bottom_StackPanel.AddHandler(Button.PreviewMouseDownEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_PreviewMouseDown))
        Me.Bottom_StackPanel.AddHandler(Button.PreviewMouseUpEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_PreviewMouseUp))
        Me.Bottom_StackPanel.AddHandler(Button.MouseDownEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_MouseDown))

    End Sub

    Private Sub MouseClicks_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded
        'VERY good habit to aquire, always unhook any events you hook up.

        Me.Bottom_StackPanel.RemoveHandler(Button.PreviewMouseDownEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_PreviewMouseDown))
        Me.Bottom_StackPanel.RemoveHandler(Button.PreviewMouseUpEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_PreviewMouseUp))
        Me.Bottom_StackPanel.RemoveHandler(Button.MouseDownEvent, New RoutedEventHandler(AddressOf Bottom_StackPanel_MouseDown))

    End Sub

End Class
